<?php

class ErrorController extends SGN_Controller
{
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        /*
        $scribe_xhprof = SGN_Scribe::factory('Error');
        $scribe_xhprof->write(
                date('Ymd H:i:s', SGN_Application::getCurrentTime()) . "\t" . 
                $errors->exception . "\t" . 
                'Request Parameters:' . var_export($errors->request->getParams(), 1) . "\t" .
                $_SERVER['QUERY_STRING'] . "\t" .
                $_SERVER['SERVER_ADDR'] . "\t" .
                $_SERVER['HTTP_REFERER'] . "\t" .
                $_SERVER['REMOTE_ADDR'] . "\t" .
                "\n\n"
            );
            */
        if (! $errors || ! $errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Page not found';
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
                break;
        }
//         $debugConfig = SGN_Application::getConfig('myrun', 'debug');
//         SGN_Log::writeSimpleLog($errors->exception . "\n" . 'Request Parameters:' . var_export($errors->request->getParams(), 1) . "\n", 3, $debugConfig->get('exceptionLog'));
        $isAjaxRequest = $this->getRequest()->isXmlHttpRequest();
		if ($isAjaxRequest)
		{
	        $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
		}
		else 
		{
            $this->view->exception = $errors->exception;
            $this->view->request   = $errors->request;
		}
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if ($bootstrap) {
            if (! $bootstrap->hasResource('Log')) {
                return false;
            }
            $log = $bootstrap->getResource('Log');
            return $log;
        }
    }
}

